import VirtualList from "../../ui/VirtualList";
import DataUtil from "../../util/DataUtil";
import TableRowItem, { TableRowEvent } from "./TableRowItem";

const {ccclass, property} = cc._decorator;

export class TableCfgItem {
    key: string = '';
    title: string = '';
    witdh: number = 100;
    isSort: boolean = false;
    dataType: string = 'string';
}

export class TableClickEvent {
    eventKey: string = '';
    paramKey: string = '';
}

export class TableCfgJson {
    tableCfg: TableCfgItem[] = [];
    clickEvent: TableClickEvent = null;
    uniqueKey: string = '';
    initJson(json) {
        this.tableCfg = DataUtil.convertToArray<TableCfgItem>(json.data);
        this.clickEvent = DataUtil.convertToType<TableClickEvent>(json.clickEvent);
        this.uniqueKey = json.uniqueKey;
    }
}

@ccclass
export default class TableLayout extends cc.Component {

    @property(cc.JsonAsset)
    cfgJson: cc.JsonAsset = null;

    @property(VirtualList)
    virList: VirtualList<TableRowItem> = null;

    @property(TableRowItem)
    tableTitle: TableRowItem = null;

    // LIFE-CYCLE CALLBACKS:
    private _cfg: TableCfgJson = null;
    
    private _init = false;

    private _data: any[] = [];
    private _rankTypes: Map<string, boolean> = new Map<string, boolean>();
    onLoad () {
        this.init();
    }

    protected onEnable(): void {
        cc.director.on('TableRowRankEvent', this.onTableRowRankEvent, this);
    }

    protected onDisable(): void {
        cc.director.off('TableRowRankEvent', this.onTableRowRankEvent, this);
    }


    init() {
        if (this._init == false) {
            this._init = true;
            this._cfg = new TableCfgJson();
            this._cfg.initJson(this.cfgJson.json);
            this.tableTitle.showTableCfg(this._cfg.tableCfg, this._cfg.uniqueKey);
        }
    }

    showTableData (data: any[]) {
        this.init();
        this._data = data;
        let args = [];
        for (let index = 0; index < data.length; index++) {
            const element = data[index];
            args.push({
                ts_code: element.ts_code, 
                rowData: element, 
                cfgs: this._cfg.tableCfg, 
                clickEvent: this._cfg.clickEvent,
                uniqueKey: this._cfg.uniqueKey
            });
        }
        this.virList.argsArr = args;
    }

    onTableRowRankEvent (event: TableRowEvent) {
        if (event.uniqueKey == this._cfg.uniqueKey) {
            let key = event.eventKey;
            let data = this._data.slice();
            let isreverse = this._rankTypes[key] == true;
            data.sort((a, b) => {
                let defaultValue = isreverse ? -1000000000000 : 1000000000000;
                let valueA = a[key] == null ? defaultValue : a[key];
                let valueB = b[key] == null ? defaultValue : b[key];
                return isreverse ? valueB - valueA : valueA - valueB;
            });
            this.showTableData(data);
            this._rankTypes[key] = !isreverse;
        }
    }

    // update (dt) {}
}
